<template>
  <div class="btn-list center" :style="{ backgroundColor: modelValue?.bgColor }" @mouseenter="isHover = true" @mouseleave="isHover = false">
    <div class="btn-list__container" :class="[modelValue?.width ? `container-content-${modelValue?.width}` : 'container-content']">
      <btn-list :list="modelValue?.data"></btn-list>
      <!-- <div class="btn-list__row">
        <div class="btn-list__item" v-for="item in modelValue?.data" :key="item.id">
          <a :href="item.url || '#' " class="size-normal btn-primary" :class="[item.type ? `btn-primary--${item.type}` : '']">
            <div class="btn-primary__text">{{ item.text }}</div> 
            <span class="btn-primary__icon"></span>
          </a>
        </div>
      </div> -->
    </div>

    <Operate v-if="!isPreview" v-model:show="isHover" @handle-edit="showSetting = true" @handle-delete="handleDelete" @handle-copy="handleCopy" @handle-paste="handlePaste"></Operate>
  </div>

  <Setting v-if="!isPreview" v-model:show="showSetting" :data="modelValue"></Setting>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { randomString } from '@/utils/common'
import Setting from './setting.vue'
import Operate from '@/common/Operate/index.vue'
import btnList from './btnList.vue'

// 定义组件名称
defineOptions({
  name: 'BtnList'
})

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {}
  },

  // 是否预览
  isPreview: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['update:modelValue', 'handleDelete', 'handleCopy', 'handlePaste'])

const handleInit = () => {
  if (props.modelValue && props.modelValue.data) return // 有数据不用初始化
  const data = {
    id: props.modelValue?.id || randomString(),
    type: 'BtnList',
    width: '',
    bgColor: '',
    data: [
      {
        id: randomString(),
        type: '',
        text: '了解更多',
        url: 'https://www.baidu.com',
        isExternal: false,
      },
      {
        id: randomString(),
        type: 'white',
        text: '了解更多',
        url: 'https://www.baidu.com',
        isExternal: false,
      },
      {
        id: randomString(),
        type: 'light',
        text: '了解更多',
        url: 'https://www.baidu.com',
        isExternal: false,
      },
      {
        id: randomString(),
        type: 'blue',
        text: '了解更多',
        url: 'https://www.baidu.com',
        isExternal: false,
      },
    ]
  }
  emit('update:modelValue', data)
} 

const isHover = ref<boolean>(false);
const showSetting = ref<boolean>(false);

const handleDelete = () => {
  emit('handleDelete', props.modelValue.id)
}

const handleCopy = () => {
  emit('handleCopy', props.modelValue)
}

const handlePaste = (type: string) => {
  emit('handlePaste', type)
}

handleInit()
</script>

<style lang="scss" scoped>
.btn-list {
  position: relative;
}
</style>